package org.youchan.aco;

import java.awt.Image;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.youchan.aco.model.Member;
import org.youchan.aco.model.MemberProfile;
import org.youchan.aco.ui.UIManager;

abstract public class ImageLoader {
	private static final Logger LOGGER = Logger.getLogger(ImageLoader.class.getCanonicalName());
	
	private BlockingQueue<Member> mImageQueue;

	public ImageLoader() {
		mImageQueue = new LinkedBlockingQueue<Member>();
		new Thread() {
			public void run() {
				while (true) {
					try {
						Member member = mImageQueue.take();
						MemberProfile profile = member.getProfile();
						Image image = UIManager.getInstance().getImage(profile.getImageURL());
						imageLoaded(image, member);
					} catch (Exception e) {
						LOGGER.log(Level.WARNING, this.getName(), e);
					}
				}
			}
		}.start();
	}
	
	public void loadImageByMember(Member member) {
		try {
			mImageQueue.put(member);
		} catch (InterruptedException e) {
			LOGGER.log(Level.ALL, e.getLocalizedMessage(), e);
		}
	}

	abstract public void imageLoaded(Image image, Member member);
}
